package com.atguigu.springwebdemo.controller;

import com.atguigu.springwebdemo.bean.Employee;
import com.atguigu.springwebdemo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Smexy on 2022/9/7
 */
@RestController
public class EmployeeController
{

    @Autowired // 从容器中找一个EmployeeService类型的对象，赋值给当前变量
    private EmployeeService employeeService;

    @PostMapping(value = "/emp")
    public Object handle1(String op,Integer id,String lastname,String gender,String email){


        //封装数据模型
        Employee employee = new Employee(id, lastname, gender, email);

        switch (op) {
            case "select":  if (id == null){
                                return "id非法!";
                            }else {
                //查出来，e不为null，查不出，e为null
                Employee e = employeeService.getEmployeeById(id);

                return e == null ? "查无此人!" : e;
            }

            case "delete" :if (id == null){
                             return "id非法!";
                    }else {
                employeeService.deleteEmployeeById(id);

                return "success!";
            }

            case "update":  if (id == null){
                return "id非法!";
            }else {
                employeeService.updateEmployee(employee);

                return "success!";
            }

            case "insert":  employeeService.insertEmployee(employee);

                 return "success!";
            default: return "success!";

        }



    }

    @GetMapping(value = "/getAllEmp")
    public Object handle2(){
        List<Employee> all = employeeService.getAll();

        return all;
    }
}
